package com.dhcc.core.cxf.util;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

/**
 * webservice接口工具
 * @author cyf 2017/7/24 22:01
 */
public class CxfUtil {
	public static void main(String[] args) {
		invoke("http://localhost/ws/HelloWorld?wsdl","sayHi","Leftso");
	}
	/**
	 * 调用方式
	 * @param url
	 * @param method
	 * @param parms
	 */
	public static Object invoke(String url,String method,Object...parms) {
        // 创建动态客户端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(url);
        // 需要密码的情况需要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME,
        // PASS_WORD));
        try {
            // invoke("方法名",参数1,参数2,参数3....);
        	Object[] res = client.invoke(method, parms);
            return res[0];
        } catch (java.lang.Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}